<?php

namespace App\Http\Controllers;

use App\Person;
use Illuminate\Cache\CacheManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class HomeController extends Controller
{
    /**
     * @var CacheManager
     */
    public $cache;
    
    public function __construct(CacheManager $cache)
    {
        $this->cache = $cache;
    }
    
    public function index()
    {
        $persons = Person::all();
        $persons = Person::whereAge(24)->get();
        return view('person', ['persons' => $persons]);
    }
    
    public function show()
    {
        $key_name = 'hello';
        if (Cache::has($key_name)) {
            $val = Cache::get($key_name);
            dump($val);
        } else {
            $val = 'world';
            Cache::put($key_name, $val, 500);
        }
        
        dump(Cache::get($key_name));
        if ($this->cache->has('persons')) {
            dump($this->cache->get('persons'));
        }
        $res = $this->cache->has('persons');
        dump($res);
        
        return $this->cache->remember('persons', 320, function () {
            return  Person::whereAge(24)->get();
        });
    }
    public function cache()
    {
        $key = 'hash_person';
        $persons = Person::whereAge(24)->get();
        $persons->map(function ($person) use ($key){
            return Redis::hSet($key, $person->id, json_encode($person));
        });
        dd($persons);
    }
}
